import { configureStore } from '@reduxjs/toolkit'

import accountReducer from '../features/account/accountSlices'

import { throttle, localStore } from '../utils'

const preloadedState = JSON.parse(localStore.get('redux-store') || '{}')

const store = configureStore({
  reducer: {
    accout: accountReducer
  },
  preloadedState
})

store.subscribe(
  throttle(() => {
    localStore.set('redux-store', JSON.stringify(store.getState()))
  }, 1000)
)

export type RootState = ReturnType<typeof store.getState>
export type AppDispatch = typeof store.dispatch

export default store
